$(document).ready(function() {
    $("#convert-files-confirmation-modal").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width:'400px',
        maxHeight:'600px',
        title:'File Conversion',
        buttons: {
            Ok: function(){
                $(this).dialog( "close" );
            }
        }
    }).css("font-size", "12px");
    
    $("#delete-bucket-modal").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width:'400px',
        maxHeight:'600px',
        title:'Delete bucket',
        buttons: {
            Ok: function(){
                $("#removeBucketForm").submit();
                $(this).dialog( "close" );
            },
            Cancel: function(){
                $(this).dialog( "close" );
            }
        }
    }).css("font-size", "12px");
    
    $("#add-bucket-modal").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width:'400px',
        maxHeight:'600px',
        title:'Create bucket',
        buttons: {
            Ok: function(){
                $("#newBucketForm").submit();
                $(this).dialog( "close" );
            },
            Cancel: function(){
                $(this).dialog( "close" );
            }
        }
    }).css("font-size", "12px");
    
    $("#convert-files-modal").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width:'400px',
        maxHeight:'600px',
        title:'File Conversion',
        buttons: {
            Ok: function(){
                $.ajax({
                    url: "/spssconverter/convert.do",
                    type: 'POST',
                    data: $(this).data("params"),
                }).done(function(data) {
                    var msg = "";
                    if(data = "true"){
                        msg = "File conversion done";
                    } else {
                        msg = "The file conversion has failed. Please try it again later";
                    }
                    $("#convert-files-confirmation-modal").find(".msg").html(msg);
                    $("#convert-files-confirmation-modal").dialog("open");
                });
                $("#convert-files-modal").dialog( "close" );
            },
            Cancel: function(){
                $(this).dialog( "close" );
            }
        }
    }).css("font-size", "12px");
    
    $("#delete-object-modal").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width:'400px',
        maxHeight:'600px',
        title:'Delete object',
        buttons: {
            Ok: function(){
                var bucketName = $("#delete-object-modal").find("#bucket-name").val();
                var objectName = $("#delete-object-modal").find("#delete-object-name").val();
                var params = {
                    bucketName:bucketName,
                    objectName:objectName
                }
                $.ajax({
                  url: "/s3/deleteObject.do",
                  type: 'POST',
                  data: params,
                }).done(function(data) {
                    $("tr").find("div").each(function(){
                        if($(this).attr("id")==objectName){
                            $(this).parent().parent().remove();
                        }
                    });
                    $("#delete-object-modal").dialog("close");
                });
            },
            Cancel: function(){
                $(this).dialog( "close" );
            }
        }
    }).css("font-size", "12px");
    
    $(".btn_remove_bucket").on("click",function(){
        var msg = "The bucket <strong>"+$(this).data("id")+"</strong> is going to be removed.Are you sure?"
        $("#delete-bucket-modal").find(".msg").html(msg);
        $("#delete-bucket-modal").find("#delete-bucket-name").val($(this).data("id"));
        $("#delete-bucket-modal").dialog("open");
    });
    
    $("#add-bucket-btn").on("click",function(){
        $("#add-bucket-modal").dialog("open");
    });
    
    $(".btn_remove_object").on("click",function(){
        var msg = "The object <strong>"+$(this).data("id")+"</strong> is going to be removed.Are you sure?"
        $("#delete-object-modal").find(".msg").html(msg);
        $("#delete-object-modal").find("#delete-object-name").val($(this).data("id"));
        $("#delete-object-modal").find("#bucket-name").val($(this).data("bucket"));
        $("#delete-object-modal").dialog("open");
    });
    
    $("#uploadForm").submit(function() {
        if($("#btn-submit-upload").hasClass("disabled")){
            return false;
        }
        if($("#path").val()!="" && String($("#path").val()).lastIndexOf("/") == -1){
            $("#path-validation").addClass("text-error").html("Paths must end with '/'");
            return false;
        }
        var bucketPath= $("#bucketPath").val() + $("#path").val();
        $("#bucketPath").attr('name','bucketPath:'+bucketPath);
        return true;
    });
    
    $('input[id=file]').change(function() {
        $('#browseFile').val($(this).val().split(/(\\|\/)/g).pop());
        if($('#browseFile').val()!=""){
            $('#btn-submit-upload').removeClass("disabled");
        }
     });
     
    $("#btn-add-file").on("click",function(){
        $("#btn-add-file").toggleClass("active");
        $("#uploadForm").toggle("fast");
    });
    
    $("#btn-convert-folder").on("click",function(){
        if($(this).hasClass("disabled")){
            return false;
        }
        var files="";
        $("table").find(".object").each(function(){
            var file=$(this).attr("id");
            if(file.indexOf(".sav")!=-1){
                var splitFile = file.split("/");
                files = files + splitFile[splitFile.length -1] + "|";
            }
        });
        var params = {
            inputBucket:$(this).data("bucketname"),
            inputKey:$(this).data("prefix"),
            outputBucket:$(this).data("bucketname"),
            outputKey:$(this).data("prefix"),
            conversionScope:"isfolder",
            conversionType:"headersonly",
            files:files
        };
        var msg = "All the .sav files in this folder are going to be converted to .csv. Are you sure?";
        $("#convert-files-modal").find(".msg").html(msg);
        $("#convert-files-modal").data("params",params);
        $("#convert-files-modal").dialog("open");
    });
    
    $(".btn_convert_single_file").on("click",function(){
        var params = {
            inputBucket:$(this).data("bucketname"),
            inputKey:$(this).data("prefix"),
            outputBucket:$(this).data("bucketname"),
            outputKey:$(this).data("prefix"),
            conversionScope:"isfile",
            conversionType:"headersonly"
        };
        var split_result = params.inputKey.split("/");
        var msg = "You are going to convert the "+split_result[split_result.length-1]+" file to .csv extension. Are you sure?";
        $("#convert-files-modal").find(".msg").html(msg);
        $("#convert-files-modal").data("params",params);
        $("#convert-files-modal").dialog("open");
    });
});